//
//

#import "CellForMessage.h"
#import "YHWorkGroupPhotoContainer.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"

/***发布动态视图**/

const CGFloat contentLabelFontSize1 = 13.0;
CGFloat maxContentLabelHeight1 = 0;  //根据具体font而定
CGFloat kMarginContentLeft1    = 10; //动态内容左边边距
CGFloat kMarginContentRight1   = 10; //动态内容右边边边距
const CGFloat moreBtnHeight1   = 20;
const CGFloat moreBtnWidth1    = 60;

@interface CellForMessage()
@property (nonatomic,strong)UIImageView *imgvAvatar;
@property (nonatomic,strong)UILabel     *labelContent;
@property (nonatomic,strong)UILabel     *labelDelete;
@property (nonatomic,strong)UILabel     *labelMore;
@property (nonatomic,strong)UILabel     *labelBottomLeft;
@property (strong,nonatomic)UIView      *bottomView;
@property (strong,nonatomic)UIView      *lineView;
@property (strong,nonatomic)UIView      *leftView;

@property (nonatomic,strong)YHWorkGroupPhotoContainer *picContainerView;

@property (nonatomic,strong)UIView      *viewSeparator;

//约束
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbMore;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstCenterYlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstLeftlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbContent;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightPicContainer;
@property (nonatomic,strong)NSLayoutConstraint *cstTopPicContainer;
@property (nonatomic,strong)NSLayoutConstraint *cstTopViewBottom;
@end

@implementation CellForMessage

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setup{
    
    self.leftView = [[UIView alloc]init];
    self.leftView.layer.borderColor  = UIColors(0xDFDFDF).CGColor;//边框颜色
    self.leftView.layer.borderWidth  = 1.1;//边框宽度
    self.leftView.layer.cornerRadius  = 5; // 圆角的弧度
    self.leftView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.leftView];

    
    self.labelContent = [UILabel new];
    self.labelContent.font = [UIFont systemFontOfSize:contentLabelFontSize1];
    self.labelContent.numberOfLines = 0;
    self.labelContent.font = [UIFont systemFontOfSize:16.0f];
    
    [self.contentView addSubview:self.labelContent];
    
    self.labelMore = [UILabel new];
    self.labelMore.font = [UIFont systemFontOfSize:14.0f];
    self.labelMore.textColor = [UIColor grayColor];
    self.labelMore.userInteractionEnabled = YES;
    UITapGestureRecognizer *moreTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMoreTap)];
    [self.labelMore addGestureRecognizer:moreTap];
    [self.contentView addSubview:self.labelMore];
    
    
    self.picContainerView = [[YHWorkGroupPhotoContainer alloc] initWithWidth:MAIN_SCREEN_WIDTH - 20 - 80];
    [self.contentView addSubview:self.picContainerView];
    
    
    self.bottomView = [[UIView alloc]init];
    
    self.labelBottomLeft = [UILabel new];
    self.labelBottomLeft.font = [UIFont systemFontOfSize:14.0f];
    self.labelBottomLeft.textColor = [UIColor grayColor];
    [self.bottomView addSubview:self.labelBottomLeft];
    
    self.labelDelete = [UILabel new];
    self.labelDelete.font = [UIFont systemFontOfSize:14.0f];
    self.labelDelete.textColor = [UIColor redColor];
    self.labelDelete.userInteractionEnabled = YES;
    UITapGestureRecognizer *deleteTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteTap)];
    [self.labelDelete addGestureRecognizer:deleteTap];
    [self.bottomView addSubview:self.labelDelete];
    
    
    [self.contentView addSubview:self.bottomView];
    
    self.imgvAvatar = [UIImageView new];
    self.imgvAvatar.layer.masksToBounds = YES;
    self.imgvAvatar.userInteractionEnabled = YES;
    [self.contentView addSubview:self.imgvAvatar];
    
    [self layoutUI];
}

- (void)layoutUI{
    __weak typeof(self)weakSelf = self;
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(18.9);
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-60);
        make.bottom.equalTo(self.contentView.mas_bottom);
    }];
   
    _cstHeightlbContent = [NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstHeightlbContent];
    [self.labelContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.leftView.mas_top).offset(10);
        make.left.equalTo(weakSelf.leftView).offset(10);
        make.right.equalTo(weakSelf.leftView).offset(-10);
        make.bottom.equalTo(weakSelf.labelMore.mas_top).offset(-11);
    }];
    
    // 不然在6/6plus上就不准确了
    self.labelContent.preferredMaxLayoutWidth = MAIN_SCREEN_WIDTH - 20;
    
    _cstHeightlbMore = [NSLayoutConstraint constraintWithItem:self.labelMore attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstHeightlbMore];
    [self.labelMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.labelContent.mas_bottom).offset(0);
        make.left.equalTo(weakSelf.leftView.mas_left).offset(10);
        make.width.mas_equalTo(80);
    }];
    
    _cstHeightPicContainer = [NSLayoutConstraint constraintWithItem:self.picContainerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstHeightPicContainer];
    
    _cstTopPicContainer = [NSLayoutConstraint constraintWithItem:self.picContainerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelMore attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    [self.contentView addConstraint:_cstTopPicContainer];
    [self.picContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftView.mas_left).offset(10);
        make.right.mas_greaterThanOrEqualTo(self.leftView.mas_right).offset(-10);
    }];
    [self.picContainerView setContentHuggingPriority:249 forAxis:UILayoutConstraintAxisVertical];
    [self.picContainerView setContentCompressionResistancePriority:749 forAxis:UILayoutConstraintAxisVertical];
    
    _cstTopViewBottom = [NSLayoutConstraint constraintWithItem:self.bottomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.picContainerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
    _cstTopViewBottom.priority = UILayoutPriorityDefaultLow;
    [self.contentView addConstraint:_cstTopViewBottom];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftView.mas_left);
        make.right.equalTo(self.leftView.mas_right);
        make.height.mas_equalTo(44);
    }];
    
    [self.labelDelete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bottomView.mas_right).offset(-10);
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    
    [self.labelBottomLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomView.mas_left).offset(10);
        make.centerY.equalTo(self.bottomView.mas_centerY);
        make.right.equalTo(self.labelDelete.mas_left);
    }];
    
    
    [self.imgvAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(15);
        make.left.equalTo(weakSelf.contentView).offset(0);
        make.width.height.mas_equalTo(20);
    }];

    
    /*******使用HYBMasonryAutoCell*******/
    [self.viewSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bottomView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(15);
    }];
    
    self.hyb_lastViewInCell = self.viewSeparator;
    
}


- (void)setModel:(YHWorkGroup *)model{
    _model = model;
    [self.imgvAvatar setImage:[UIImage imageNamed:@"messageLeft"]];
    
    maxContentLabelHeight1   = _labelContent.font.pointSize * 6;
    self.labelContent.text  = _model.msgContent;
    //查看详情按钮
    self.labelMore.text     = @"查看全部";
    CGFloat moreBtnH = 0;
    if (_model.shouldShowMoreButton) { // 如果文字高度超过60
        moreBtnH = moreBtnHeight1;
        if (_model.isOpening) { // 如果需要展开
            _labelMore.text = @"收起";
            _cstHeightlbContent.constant = HUGE;
        } else {
            _labelMore.text = @"查看全部";
            _cstHeightlbContent.constant = maxContentLabelHeight1;
        }
    }else{
        _cstHeightlbContent.constant = maxContentLabelHeight1;
    }
    
    //删除按钮
    if(model.delPass==YES){
        self.labelDelete.text   = @"删除";
        _cstLeftlbDelete.constant = moreBtnHeight1;
    }else{
        self.labelDelete.text     = @"";
        _cstLeftlbDelete.constant = 0;
    }
    //更新“查看详情”的约束
    _cstHeightlbMore.constant   = moreBtnH;
    
    CGFloat picTop = 0;
    if (moreBtnH) {
        picTop = 10;
    }else{
        picTop = 0;
    }
    
    _cstTopPicContainer.constant    = picTop;
    
    CGFloat picContainerH = [self.picContainerView setupPicUrlArray:_model.thumbnailPicUrls];
    self.picContainerView.picOriArray = _model.originalPicUrls;
    
    _cstHeightPicContainer.constant = picContainerH;
    
    CGFloat viewBottomTop = 0;
    if(_model.thumbnailPicUrls.count){
        viewBottomTop = 5;
    }
    _cstTopViewBottom.constant = viewBottomTop;
        
    [self.labelBottomLeft setText:_model.bottomLeft];

}

#pragma mark - Action
- (void)onMoreTap
{
    if (_delegate && [_delegate respondsToSelector:@selector(onMoreInCell:)]) {
        [_delegate onMoreInCell:self];
    }
}

- (void)deleteTap{
    if (_delegate && [_delegate respondsToSelector:@selector(onDeleteInCell:)]) {
        [_delegate onDeleteInCell:self];
    }
}

#pragma mark - Gesture


#pragma mark - HKPBotViewDelegate
- (void)onAvatar{
    
}

- (void)onMore{
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
